package com.yy.train.ticket.train.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.train.ticket.goout.trs.BoxSeat;
import com.yy.train.ticket.goout.TrainSeat;
import com.yy.train.ticket.train.dao.TtTrainRunDateDao;
import com.yy.train.ticket.train.dao.TtTrainRunSeatDao;
import com.yy.train.ticket.train.dao.TtTrainSeatDao;
import com.yy.train.ticket.train.entity.TtTrainRunDateEntity;
import com.yy.train.ticket.train.entity.TtTrainRunSeatEntity;
import com.yy.train.ticket.train.service.TtTrainRunSeatService;
import com.yy.train.ticket.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;


@Service("ttTrainRunSeatService")
public class TtTrainRunSeatServiceImpl extends ServiceImpl<TtTrainRunSeatDao, TtTrainRunSeatEntity> implements TtTrainRunSeatService {

@Resource
    TtTrainRunSeatDao ttTrainRunSeatDao;
    @Resource
    TtTrainSeatDao ttTrainSeatDao;
    @Resource
    TtTrainRunDateDao ttTrainRunDateDao;
    @Override
    public R saveTrainRunSeat(TtTrainRunSeatEntity trs) throws InterruptedException {
        Long runDateId = trs.getRunDateId();
        Long aLong = ttTrainRunSeatDao.selectCount(Wrappers.<TtTrainRunSeatEntity>lambdaQuery().eq(TtTrainRunSeatEntity::getRunDateId, runDateId));
        if (aLong == 0L) {
        TtTrainRunDateEntity trainRunDateEntity=   ttTrainRunDateDao.selectById(trs.getRunDateId());
        List<TrainSeat> li= ttTrainSeatDao.trainSeatlist(trs.getTrainId());
            if (!li.isEmpty()) {
        TtTrainRunSeatEntity ttTrainRunSeatEntity;
        List<TtTrainRunSeatEntity> list=new ArrayList<>();
        for (TrainSeat ts:li) {
            ttTrainRunSeatEntity=new TtTrainRunSeatEntity();
            ttTrainRunSeatEntity.setRunDateId(trainRunDateEntity.getRunDateId());
            ttTrainRunSeatEntity.setNumberId(trainRunDateEntity.getNumberId());
            ttTrainRunSeatEntity.setRunDate(trainRunDateEntity.getRunDate());
            ttTrainRunSeatEntity.setRunTime(trainRunDateEntity.getRunTime());
            ttTrainRunSeatEntity.setSeatNo(ts.getSeatNo());
            ttTrainRunSeatEntity.setTrainNo(ts.getTrainNo());
            ttTrainRunSeatEntity.setBoxNo(ts.getBoxNo());
            ttTrainRunSeatEntity.setSeatId(ts.getSeatId());
            ttTrainRunSeatEntity.setTrainId(ts.getTrainId());
            ttTrainRunSeatEntity.setBoxId(ts.getBoxId());
            ttTrainRunSeatEntity.setGrade(ts.getGrade());
            list.add(ttTrainRunSeatEntity);
        }

                // 请求总数
                int clientTotal = list.size();
                // 同时并发执行的线程数
                int threadTotal = 200;
                ExecutorService executorService = Executors.newCachedThreadPool();
                final Semaphore semaphore = new Semaphore(threadTotal);
                final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
                for (int i = 0; i < clientTotal ; i++) {
                    TtTrainRunSeatEntity ts=list.get(i);
                    executorService.execute(() -> {
                        try {
                            semaphore.acquire();
                            this.ttTrainRunSeatDao.insert(ts);
                            semaphore.release();
                        } catch (Exception e) {
                            System.out.println( e);
                        }
                        countDownLatch.countDown();
                    });
                }
                countDownLatch.await();
                executorService.shutdown();
        return R.ok();
            } else  {
            return R.error("已经生成好了座位");
        }
        }else {
            return R.error("列车已经有生成座位");
        }
    }

    @Override
    public R TrainRunSeatlist(Long runDateId) {
        //List<TtTrainRunSeatEntity> list=  this.ttTrainRunSeatDao.trainRunSeatlist(runDateId);
        return R.ok();
    }

    @Override
    public R getNumberBoxSeat(Long runDateId) {
        List<BoxSeat> list = ttTrainRunSeatDao.getTrainBoxSeat(runDateId);
        return R.ok(list);
    }


}